/**
 * Project Name:mauto-web
 * File Name:NewImageUtils.java
 * Package Name:com.inftech.mauto.common.utils
 * Date:2017年3月30日下午7:59:40
 * Copyright (c) 2017, inf-technology Co., Ltd. All Rights Reserved.
 *
*/

package com.inftech.mauto.common.utils;
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
public class NewImageUtils {
    /**
     * 
     * @Title: 构造图片
     * @Description: 生成水印并返回java.awt.image.BufferedImage
     * @param file
     *            源文件(图片)
     * @param waterFile
     *            水印文件(图片)
     * @param x
     *            距离右下角的X偏移量
     * @param y
     *            距离右下角的Y偏移量
     * @param alpha
     *            透明度, 选择值从0.0~1.0: 完全透明~完全不透明
     * @return BufferedImage
     * @throws IOException
     */
    public static void watermark(String sourceFilePath, String waterFilePath1,String waterFilePath2) throws IOException {
        // 获取底图
        BufferedImage buffImg = ImageIO.read(new URL(sourceFilePath));
        // 获取层图
        BufferedImage waterImg1 = ImageIO.read(new File(waterFilePath1));
     // 获取层图
        BufferedImage waterImg2 = ImageIO.read(new URL(waterFilePath2));
        // 创建Graphics2D对象，用在底图对象上绘图
        Graphics2D g2d = buffImg.createGraphics();
        // 在图形和图像中实现混合和透明效果
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f));
        // 绘制
        g2d.drawImage(waterImg1, 70, 415, 120, 120, null);
        g2d.drawImage(waterImg2, 115, 460, 30, 30, null);
        g2d.dispose();// 释放图形上下文使用的系统资源
        int temp = waterFilePath1.lastIndexOf(".") + 1;
        ImageIO.write(buffImg, waterFilePath1.substring(temp), new File(waterFilePath1));
    }

    /**
     * 
     * @param args
     * @throws IOException
     *             IO异常直接抛出了
     * @author bls
     */
    public static void main(String[] args) throws IOException {
        String sourceFilePath = "D:/apache-tomcat-7.0.69/wtpwebapps/mauto-web/files/qrcode/bottom.jpg";
        String waterFilePath1 = "D:/data/qrcode/2.png";
        String waterFilePath2 = "http://wx.qlogo.cn/mmopen/g8Ox1cw9ydLvrhQlIaPhydweNzMGrFTO2aibk97IvxGQ2ODpukkXaY3gciblo1iaj9RbwvZKiaUZsEgdDbHZIe6Hhhl8gmAcrGae/0";
        // 构建叠加层
        NewImageUtils.watermark(sourceFilePath, waterFilePath1, waterFilePath2);
    }
}

